using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	public class Image : System.Web.UI.WebControls.Image, IContent, IConfigurationAware, IControlDataLoadAware
	{

		private IImageConfiguration configuration;
		private string configurationId = "configuration.default.Image";

		private string relativePath;
		
		private String contentPlaceHolderId;

		protected IDataBinder binder;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { this.configuration = (IImageConfiguration) value; }
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.default.Image")]
		public string ConfigurationId
		{
			get { return configurationId; }
			set { this.configurationId = value; }
		}

		[Bindable(true), Category("Sedna")]
		public string RelativePath
		{
			get { return relativePath; }
			set { relativePath = value; }
		}


		public Image()
		{
		}

		public Image(IConfiguration configuration)
		{
			this.configuration = (IImageConfiguration)configuration;
			this.configurationId = configuration.ObjectName;
//			BuildControlFromConfiguration();
		}

		protected override void OnInit(EventArgs e)
		{
			if (configuration == null)
			{
				configuration = (IImageConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);	
			}
			Build();
			BuildControlFromConfiguration();
			binder=new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
			base.OnInit (e);
		}

		protected void BuildControlFromConfiguration()
		{
			this.ID = configuration.Id;

			if(RelativePath == null || "".Equals(relativePath))
			{
				RelativePath=configuration.ImageUrl;
			}
			//ImageUrl = configuration.ImageManager.RelateImageUrl(relativePath);
		}
	
		private void Build()
		{
			Visible = Configuration.IsVisibleForPrincipal() && Configuration.Visible;
            Enabled = Configuration.IsEnabledForPrincipal() && Configuration.Enabled;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			IDictionary executionContext=ExecutionContext.GetCurrent(ClientID);
			Unbind();
			ControlUtil.ExecuteOnLoad(executionContext, Context, Configuration, this.ClientID);

            if (Configuration.VisibleBindPath != null)
                this.Visible = true; // To reach the OnPreRender method and analyze visibility in the Bind method
		}

		public void OnDataLoad()
		{
			IDictionary executionContext=ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnDataLoad(executionContext, Context, Configuration, this.ClientID);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			IDictionary executionContext=ExecutionContext.GetCurrent(ClientID);
			ControlUtil.ExecuteOnPreRender(executionContext, Context, Configuration, this.ClientID);
			Bind();

			if(!this.Enabled && configuration.DisabledImageUrl != null)
			{
				this.ImageUrl = configuration.ImageManager.RelateImageUrl(configuration.DisabledImageUrl);
			}
			else
			{
				this.ImageUrl = configuration.ImageManager.RelateImageUrl(RelativePath);
			}
		}

		private void Bind()
		{
            this.Visible = this.configuration.Visible;
		    
            binder.Bind(new string[] { "ImageUrl", "AlternateText", "Enabled", "Visible" });

            Enabled = Enabled && configuration.IsEnabledForPrincipal();
            Visible = Visible && configuration.IsVisibleForPrincipal();
		}

		private void Unbind()
		{
            binder.Unbind(new string[] { "ImageUrl", "AlternateText", "Enabled", "Visible" });	
		}


	}
}
